package com.neolix.udpserver;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.WindowManager;

public class Utils {

    /**
     * Get Display（限Activity类型的Context）
     * @param activity Activity for get WindowManager
     * @return Display
     */
    private static Display getDisplay(Activity activity) {
        //WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        WindowManager wm = activity.getWindowManager();
        if (wm != null) {
            return wm.getDefaultDisplay();
        } else {
            return null;
        }
    }

    public static int getScreenHeight(Activity context) {
        return getDisplay(context).getHeight();
    }

    public static int getScreenWidth(Activity context) {
        return getDisplay(context).getWidth();
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
